<?php
namespace api\modules\v1\models;

use api\components\ApiModel;
use common\models\ARMessagePrompt;
use Yii;
use yii\helpers\ArrayHelper;
use common\models\ARNeed;
use common\models\ARCoinType;

/**
 * 消息提示模型
 * @author WJ
 */
class MessagePrompt extends ApiModel
{
    const SCENARIO_LIST = 'list';
    const SCENARIO_SET_READ = 'set_read';

    public $prompt_ids;
    public $is_read;
    public $size;
    public $page;

    const READ_ALL = -2;

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'prompt_ids' => '提示消息编号',
            'is_read'    => '是否已读',
            'size'       => '分页大小',
            'page'       => '当前页',
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            self::SCENARIO_LIST     => ['is_read', 'size', 'page'],
            self::SCENARIO_SET_READ => ['prompt_ids'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['is_read', 'size', 'page'], 'trim'],
            [['prompt_ids'], 'required'],
            ['is_read', 'default', 'value' => self::READ_ALL],
            ['is_read', 'in', 'range' => [ARMessagePrompt::READ_NOT, ARMessagePrompt::READ_IS, self::READ_ALL]],
            ['size', 'default', 'value' => 10],
            ['page', 'default', 'value' => 1],
        ];
    }

    /**
     * 添加提示纪录
     */
    public static function addPrompt($param, $obj, $uid = null, $order_id = null)
    {
        $uid = $uid?$uid:Yii::$app->user->id;
        $message_detail = '';
        switch ($param) {
            case 'name-auth':   //实名验证通过
                $message_detail = $message = ARMessagePrompt::$params[$param]['message'];
                break;
            case 'apply-broker':    //申请成为经纪人
                $message_detail = $message = ARMessagePrompt::$params[$param]['message'];
                break;
            case 'cancel-broker':   //取消成为经纪人
               $message_detail = $message = ARMessagePrompt::$params[$param]['message'];
                break;
            case 'join-need':   //加入广告
                $message = sprintf(ARMessagePrompt::$params[$param]['message'], $obj->nickname);
                break;
            case 'select-broker':   //被选为经纪人
                    $message = sprintf(ARMessagePrompt::$params[$param]['message'], $obj->nickname);
                break;
            default:
                $type = -1;
                $message = 'not found';
                break;
        }
        if (!isset($type)) {
            $type = ARMessagePrompt::$params[$param]['type'];
        }
        $prompt = new ARMessagePrompt();
        $prompt->uid = $uid;
        $prompt->type = $type;
        $prompt->order_id = $order_id;
        $prompt->message = $message;
        $prompt->message_detail = $message_detail;
        if (!$prompt->save()) {
            self::getErrorMessge($prompt);
        }
    }

    /**
     * 提示记录列表
     */
    public function messageList()
    {
        if (!$this->validate()) {
            self::getErrorMessge($this);
        }
        $read_type = ($this->is_read == self::READ_ALL) ? [ARMessagePrompt::READ_NOT, ARMessagePrompt::READ_IS] : $this->is_read;
        $sql = ARMessagePrompt::find()->where(['uid' => Yii::$app->user->id, 'is_read' => $read_type]);
        $count = $sql->count();
        $not_count = ARMessagePrompt::find()->where(['uid' => Yii::$app->user->id, 'is_read' => ARMessagePrompt::READ_NOT])->count();
        $result = $sql->limit($this->size)->offset($this->size * ($this->page - 1))->orderBy(['created_at' => SORT_DESC])->all();
        $rows = ArrayHelper::toArray($result, [
            ARMessagePrompt::className() => [
                'id',
                'type',
                'order_id',
                'message',
                'message_detail',
                'is_read',
                'created_time' => function ($row) {
                    return Yii::$app->formatter->asDatetime($row->created_at);
                },
                'updated_time' => function ($row) {
                    return Yii::$app->formatter->asDatetime($row->updated_at);
                },
            ],
        ]);
        return ['count' => $count, 'not_count' => $not_count,'rows' => $rows];
    }

    /**
     * 设置消息为已读
     */
    public function messageDefault()
    {
        if (!$this->validate()) {
            self::getErrorMessge($this);
        }
        if ($this->prompt_ids == (string)self::READ_ALL) {
            $result = ARMessagePrompt::find()->select(['id'])->where(['uid' => Yii::$app->user->id, 'is_read' => ARMessagePrompt::READ_NOT])->asArray()->all();
        } else {
            $prompt_ids = explode(',', $this->prompt_ids);
            $result = ARMessagePrompt::find()->select(['id'])->where(['id' => $prompt_ids, 'uid' => Yii::$app->user->id, 'is_read' => ARMessagePrompt::READ_NOT])->asArray()->all();
        }
        $num = ARMessagePrompt::updateAll(['is_read' => ARMessagePrompt::READ_IS], ['id' => $result]);
        return ['num' => $num];
    }
}
